package com.order.controller;

import com.order.dao.DeskDao;
import com.order.pojo.Order;
import com.order.service.CustomerService;
import com.order.service.DeskService;
import com.order.service.OrderListService;
import com.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderListService orderListService;
    @Autowired
    private DeskService deskService;
    @Autowired
    private CustomerService customerService;

    @ResponseBody
    @RequestMapping("start")
    public Integer startEating(Order order) {
        order.setTime(new Timestamp(System.currentTimeMillis()));
        order.setState(0);
        //修改桌子状态
        //需要判断桌子的状态
        deskService.setFull(order.getDesk_id());
        orderService.startEating(order);
        return  order.getId();
    }

    @ResponseBody
    @RequestMapping("end")
    public String endEating(Integer order_id) {
        Double money=orderListService.sumPrice(order_id);
        System.out.println(order_id);
        System.out.println(money);
        //修改桌子状态
        deskService.setEmpty(orderService.deskId(order_id));
        //扣费
        //查出用户id
        Integer customerId = orderService.customerId(order_id);
        customerService.pay(customerId,money);
        Boolean flag = orderService.endEating(money, order_id);
        return "共消费"+money+"元,已经从您的账户余额中扣除";

    }

}
